#pragma once
#include <string>

#include <libnuraft/nuraft.hxx>
#include "vector_database.h"
using namespace nuraft;
class RaftStuff{
public:
    // 构造函数
    RaftStuff(int node_id,const std::string& endpoint,int port,VectorDatabase* vector_database);

    // 初始化结点
    void init();

    // raft集群添加新结点
    ptr<cmd_result<ptr<buffer>>> addSrv(int srv_id,const std::string& endpoint);

    void enableElectionTimeout(int lower_bound,int upper_bound);
    std::vector<std::tuple<int, std::string, std::string, nuraft::ulong, nuraft::ulong>> getAllNodesInfo() const;
    std::tuple<int,std::string,std::string,nuraft::ulong,nuraft::ulong> getCurrentNodesInfo() const;

    std::string getNodeStatus(int node_id)const;
    ptr<cmd_result<ptr<buffer>>> appendEntries(const std::string& entry);

    // 判断是否为主结点
    bool is_leader()const;
private:
    int node_id;
    std::string endpoint;
    int port_;
    ptr<state_mgr> smgr_;// 状态管理器对象指针
    ptr<state_machine> sm_;// 状态机对象指针
    raft_launcher launcher_;//raft启动器对象
    ptr<raft_server> raft_instance_;// raft实例对象指针
    VectorDatabase* vector_database;
};